package io.lkt.modules.sys.controller;

import io.lkt.common.util.Query;
import io.lkt.common.utils.DateUtil;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.modules.sys.entity.ButtonEventInfo;
import io.lkt.modules.sys.entity.SysUserEntity;
import io.lkt.modules.sys.service.BUserInfoService;
import io.lkt.modules.sys.service.ButtonEventService;
import io.lkt.modules.sys.service.SysGzhService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/buttonevent")
public class ButtonEventController extends AbstractController {

    @Autowired
    private ButtonEventService buttonEventService;

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) throws ParseException {
        /*if (StringUtils.isBlank(params.get("gzhCode").toString())) {
            return R.error("参数异常");
        }*/

        String eventCode = null;
        String gzhCode = null;
        String startDate = null;
        String endDate = null;
        Integer userId = null;
        Integer followStatus = null;

        if (params.get("eventCode") != null) {
            eventCode = params.get("eventCode").toString();
        }

        if (params.get("gzhCode") != null) {
            gzhCode = params.get("gzhCode").toString();
        }

        if (params.get("startDate") != null) {
            startDate = params.get("startDate").toString();
        }

        if (params.get("endDate") != null) {
            // 日期格式化对象
//            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//            Date eDate = dateFormat.parse(params.get("endDate").toString());
//
//            endDate = dateFormat.format(DateUtil.addDay(eDate, 1));
            endDate = params.get("endDate").toString();
        }

        if (params.get("userId") != null && StringUtils.isNumeric(params.get("userId").toString())) {
//            userId = (Integer) params.get("userId");
            userId = Integer.valueOf(params.get("userId").toString());
        }

        if (params.get("followStatus") != null) {
            followStatus = Integer.valueOf(params.get("followStatus").toString());
        }

        Query query = new Query(params);
        List<ButtonEventInfo> list = null;
        int total = 0;
        try {
            list = buttonEventService.queryList(query.getPage(), query.getLimit(), eventCode, gzhCode, startDate, endDate, userId, followStatus);
            if (CollectionUtils.isNotEmpty(list)) {
                total = list.size();
            }
        } catch (IOException | ParseException e) {

        }
        PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page", page);
    }

    @RequestMapping("/list2")
    public R list2(@RequestParam Map<String, Object> params) throws ParseException {
        if (StringUtils.isBlank(params.get("gzhCode").toString())) {
            return R.error("参数异常");
        }

        String eventCode = null;
        String gzhCode = params.get("gzhCode").toString();
        String startDate = null;
        String endDate = null;
        Integer userId = null;

        if (params.get("eventCode") != null && !params.get("eventCode").toString().equals("")) {
            eventCode = params.get("eventCode").toString();
        }

        if (params.get("startDate") != null) {
            startDate = params.get("startDate").toString();
        }

        if (params.get("endDate") != null) {
            endDate = params.get("endDate").toString();
        }

        if (params.get("userId") != null && StringUtils.isNumeric(params.get("userId").toString())) {
//            userId = (Integer) params.get("userId");
            userId = Integer.valueOf(params.get("userId").toString());
        }

        Query query = new Query(params);
        List<ButtonEventInfo> list = null;
        int total = 0;
        try {
            list = buttonEventService.queryList2(query.getPage(), query.getLimit(), eventCode, gzhCode, startDate, endDate, userId);
            if (CollectionUtils.isNotEmpty(list)) {
                total = list.size();
            }
        } catch (IOException | ParseException e) {

        }
        PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page", page);
    }

    @RequestMapping("/queryAggregation")
    public R queryAggregation(@RequestParam Map<String, Object> params) throws ParseException {
        if (StringUtils.isBlank(params.get("gzhCode").toString())) {
            return R.error("参数异常");
        }

        String eventCode = null;
        String gzhCode = params.get("gzhCode").toString();
        String startDate = null;
        String endDate = null;
        Integer userId = null;

        if (params.get("eventCode") != null && !params.get("eventCode").toString().equals("")) {
            eventCode = params.get("eventCode").toString();
        }

        if (params.get("startDate") != null) {
            startDate = params.get("startDate").toString();
        }

        if (params.get("endDate") != null) {
            endDate = params.get("endDate").toString();
        }

        if (params.get("userId") != null && StringUtils.isNumeric(params.get("userId").toString())) {
//            userId = (Integer) params.get("userId");
            userId = Integer.valueOf(params.get("userId").toString());
        }

        Query query = new Query(params);
        List<ButtonEventInfo> list = null;
        int total = 0;
        try {
            list = buttonEventService.queryAggregation(eventCode, gzhCode, startDate, endDate, userId);
            if (CollectionUtils.isNotEmpty(list)) {
                total = list.size();
            }
        } catch (IOException | ParseException e) {

        }
        PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page", page);
    }

    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        ButtonEventInfo buttonEventInfo = null;
        try {
            buttonEventInfo = buttonEventService.getInfoById(id);
        } catch (IOException e) {
//            e.printStackTrace();
        } catch (ParseException e) {
//            e.printStackTrace();
        }
        return R.ok().put("buttonEventInfo", buttonEventInfo);
    }

    @RequestMapping("/updateById")
    public R updateById(@RequestBody ButtonEventInfo buttonEventInfo) {
        if (buttonEventInfo == null || buttonEventInfo.getId() == null) {
            return R.error("参数异常");
        }

        SysUserEntity user = getUser();
        Integer followUserId = Integer.valueOf(user.getUserId().toString());// 当前平台用户：跟进人ID
        String followUserName = user.getUsername();// 当前平台用户：跟进人名称

        try {
            ButtonEventInfo info = new ButtonEventInfo();
            info.setId(buttonEventInfo.getId());
            info.setFollowstatus(Integer.valueOf(1));
            info.setFollowuserid(followUserId);
            info.setFollowusername(followUserName);
            info.setRemarks(buttonEventInfo.getRemarks());

            buttonEventService.updateByIdSingle(info);
        } catch (IOException e) {
//            e.printStackTrace();
        }

        try {
            ButtonEventInfo info = new ButtonEventInfo();
            info.setEventcode(buttonEventInfo.getEventcode());
            info.setGzhcode(buttonEventInfo.getGzhcode());
            info.setUserid(buttonEventInfo.getUserid());
            info.setFollowstatus(Integer.valueOf(1));
            info.setFollowuserid(followUserId);
            info.setFollowusername(followUserName);
            info.setRemarks(buttonEventInfo.getRemarks());

            buttonEventService.batchUpdateById(info);
        } catch (IOException e) {
//            e.printStackTrace();
        } catch (ParseException e) {
//            e.printStackTrace();
        }
        return R.ok();
    }
}
